//index.js
//获取应用实例
const app = getApp()
var data = {

  position: 'menu_1',

  // 菜单列表
  menus: [],
  // 购物车
  cart: {
    totalprice: 0,
    count: 0,
    item: [],
    gouwucheimg: '../../images/shoppingCar_gray.png'
  }
};
var config = {

  // 初始数据
  data: data,

  //从服务端获取菜单列表
  onShow: function () {
    var page = this;

    var session = wx.getStorageSync('session');
    wx.request({
      url: 'http://b.vip/home/index',
      method: "POST",
      data: {
        session: session.key
      },
      dataType: 'JSON',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var result = JSON.parse(res.data);
        var first = true;
        data.menus.length = 0;
        for (var i in result) {
          if (first) first = false;
          var list = {
            title: i,
            checked: first,
            item: []
          };

          for (var n = 0; n < result[i].item.length; n++) {
            list.item[n] = result[i].item[n];
            list.item[n].count = parseInt(list.item[n].count);
            list.item[n].price = parseInt(list.item[n].price);
            list.item[n].id = parseInt(list.item[n].id);
            list.item[n].image = list.item[n].image;
          }
          data.menus.push(list);
        }

        page.setData(data);
        console.log(data);
      }
    });
  },




  // Data生效
  buildData: function () {
    for (var i = 0; i < data.menus.length; i++) {
      if (data.menus[i].checked)
        data.position = 'menu_' + i;
    }
    data.cart.count = 0;
    data.cart.item = [];
    data.cart.totalprice = 0;
    for (var i = 0; i < data.menus.length; i++) {
      for (var j = 0; j < data.menus[i].item.length; j++) {
        data.cart.count += data.menus[i].item[j].count;
        if (data.menus[i].item[j].count) {
          data.cart.item.push(data.menus[i].item[j]);
          data.cart.totalprice += (data.menus[i].item[j].price) * data.menus[i].item[j].count;
        }
      }
    }

    //将购物车的item数组写入缓存
    wx.setStorage({
      key: "cart",
      data: [data.cart.item, data.cart.totalprice]
    })

    if (data.cart.count == 0) {
      data.cart.gouwucheimg = '../../images/shoppingCar_gray.png';
    } else {
      data.cart.gouwucheimg = '../../images/shoppingCar_blue.png';
    }
    this.setData(data);
  },

  /** 查看菜品详情
  bindViewTap : function(){
      wx.navigateTo({
          url: '../foodinfo/foodinfo'
      })
  },**/

  // 切换选中分类
  scrolltotype: function (e) {
    //data.position = e.currentTarget.dataset.target;
    for (var i = 0; i < data.menus.length; i++)
      data.menus[i].checked = false;
    data.menus[e.currentTarget.dataset.index].checked = true;
    this.buildData();

  },

  // 加入购物车
  addintogouwuche: function (e) {
    var goods_id = e.currentTarget.dataset.goods;
    var menu_id = e.currentTarget.dataset.menu;
    data.menus[menu_id].item[goods_id].count++;
    data.cart.item.push(data.menus[menu_id].item[goods_id]);
    //console.log(data.cart.item);
    this.buildData();
  },

  // 移除购物车
  cutgouwuche: function (e) {
    var goods_id = e.currentTarget.dataset.goods;
    var menu_id = e.currentTarget.dataset.menu;
    if (data.menus[menu_id].item[goods_id].count) {
      data.menus[menu_id].item[goods_id].count--;
      //console.log(data.cart.item);
    }
    this.buildData();
  },
  primary: function () {
    wx.switchTab({
      url: '../orders/orders',
    })
  }
};

//获取应用实例
Page(config);

